﻿//
//  HFViewModel.m
//  HFBaseHTTPRequestManagerModule_Example
//
//  Created by admin on 2020/6/29.
//  Copyright © 2020 白野. All rights reserved.
//

#import "HFViewModel.h"
#import <sys/utsname.h>
#import <AdSupport/ASIdentifierManager.h>
#import <HFBaseHTTPRequestManagerModule/HFBaseHTTPRequestManagerModule.h>

NSString *const MobileServiceRequestValidCode = @"MobileService/requestValidCode";
NSString *const MerchantGetYZMByMobile = @"merchant/getYzmByMobile";
NSString *const MobileServiceVerifyValidCode = @"MobileService/verifyValidCode";
NSString *const MobileServiceRegistry = @"MobileService/registry";
NSString *const MobileServiceLogin = @"MobileService/login";
NSString *const MobileServiceSetLoginPass = @"MobileService/setLoginPass";
NSString *const MobileServiceResetLoginPass = @"MobileService/resetLoginPass";

@implementation HFViewModel

+ (void)loadDomainWithURLHostKey:(NSString *)URLHostKey {
    NSDictionary *dictionary = [HFURLSetting URLSetting];
    HFBaseHTTPRequestManager.manager.baseURLDictionary = dictionary[URLHostKey];
}

+ (void)reloadBaseParametersWithMobile:(NSString *)mobile mark:(NSString *)mark uid:(NSString *)uid pjid:(NSString *)pjid token:(NSString *)token {
    NSString *sbid = ASIdentifierManager.sharedManager.advertisingIdentifier.UUIDString;
    if (sbid == nil || [sbid isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
        sbid = UIDevice.currentDevice.identifierForVendor.UUIDString;
    }
    if (sbid == nil) {
        sbid = NSUUID.UUID.UUIDString;
    }
    
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *sbxh = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@"02" forKey:@"base_appid"];
    // 操作系统版本
    [parameters setValue:UIDevice.currentDevice.systemVersion ?: @"" forKey:@"base_czxtbb"];
    // 接口版本
    [parameters setValue:@"1.1" forKey:@"base_jkbb"];
    [parameters setValue:mark ?: @"" forKey:@"base_mark"];
    [parameters setValue:mobile ?: @"" forKey:@"base_mobile"];
    [parameters setValue:@"iPhone" forKey:@"base_sbcs"];
    [parameters setValue:sbid ?: @"" forKey:@"base_sbid"];
    [parameters setValue:@"I" forKey:@"base_sblx"];
    [parameters setValue:sbxh ?: @"" forKey:@"base_sbxh"];
    [parameters setValue:NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"] ?: @"" forKey:@"base_version"];
    [parameters setValue:@"02" forKey:@"appid"];
    [parameters setValue:@"3" forKey:@"container"];
    [parameters setValue:@"I" forKey:@"dt"];
    [parameters setValue:mark ?: @"" forKey:@"mark"];
    [parameters setValue:mobile ?: @"" forKey:@"mobile"];
    [parameters setValue:pjid ?: @"" forKey:@"pjid"];
    [parameters setValue:token ?: @"" forKey:@"tk"];
    [parameters setValue:uid ?: @"" forKey:@"uid"];
    [parameters setValue:NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"] ?: @"" forKey:@"version"];
    [parameters setValue:pjid ?: @"" forKey:@"walletId"];
    HFBaseHTTPRequestManager.manager.baseParameters = parameters;
}

+ (void)verificationForRegisterWithMobile:(NSString *)mobile completionHandler:(void(^)(BOOL success, id responseObject, NSError *error))completionHandler {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:mobile forKey:@"mobile"];
    [parameters setValue:mobile forKey:@"base_mobile"];
    [parameters setValue:@"C" forKey:@"type"];
    [parameters setValue:@"A" forKey:@"bt"];
    
    [HFHttpLJCFactory factory]
    .setDomain(DOMAIN_BANK)
    .setPath(MobileServiceRequestValidCode)
    .setRequest(parameters)
    .setResponseBlock(^(id<HFHttpResponseModel> resp) {
        HFHttpResonseModule *module = (HFHttpResonseModule *)resp;
        if (module.state.integerValue == 0) {
            if (completionHandler) {
                completionHandler(YES, module.responseObject, nil);
            }
        } else {
            if (completionHandler) {
                completionHandler(NO, module.responseObject, nil);
            }
        }
    })
    .setErrorBock(^(id<HFHttpFailModel> error) {
        if (completionHandler) {
            completionHandler(NO, nil, error);
        }
    })
    .post();
}

+ (void)verificationForResetPasswordWithMobile:(NSString *)mobile completionHandler:(void(^)(BOOL success, id responseObject, NSError *error))completionHandler {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:mobile forKey:@"mobile"];
    [parameters setValue:mobile forKey:@"base_mobile"];
    [parameters setValue:@"C" forKey:@"type"];
    [parameters setValue:@"B" forKey:@"bt"];
    
    [HFHttpLJCFactory factory]
    .setDomain(DOMAIN_BANK)
    .setPath(MobileServiceRequestValidCode)
    .setRequest(parameters)
    .setResponseBlock(^(id<HFHttpResponseModel> resp) {
        HFHttpResonseModule *module = (HFHttpResonseModule *)resp;
        if (module.state.integerValue == 0) {
            if (completionHandler) {
                completionHandler(YES, module.responseObject, nil);
            }
        } else {
            if (completionHandler) {
                completionHandler(NO, module.responseObject, nil);
            }
        }
    })
    .setErrorBock(^(id<HFHttpFailModel> error) {
        if (completionHandler) {
            completionHandler(NO, nil, error);
        }
    })
    .post();
}

+ (void)verificationCodeWithMobile:(NSString *)mobile completionHandler:(void(^)(BOOL success, id responseObject, NSError *error))completionHandler {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:mobile forKey:@"mobile"];
    
    [HFHttpLJCFactory factory]
    .setDomain(DOMAIN_FINANCE)
    .setPath(MerchantGetYZMByMobile)
    .setRequest(parameters)
    .setResponseBlock(^(id<HFHttpResponseModel> resp) {
        HFHttpResonseModule *module = (HFHttpResonseModule *)resp;
        if (module.state.integerValue == 0) {
            if (completionHandler) {
                completionHandler(YES, module.responseObject, nil);
            }
        } else {
            if (completionHandler) {
                completionHandler(NO, module.responseObject, nil);
            }
        }
    })
    .setErrorBock(^(id<HFHttpFailModel> error) {
        if (completionHandler) {
            completionHandler(NO, nil, error);
        }
    })
    .get();
}

+ (void)confirmVerificationCodeWithMobile:(NSString *)mobile code:(NSString *)code completionHandler:(void(^)(BOOL success, id responseObject, NSError *error))completionHandler {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:mobile forKey:@"mobile"];
    [parameters setValue:mobile forKey:@"base_mobile"];
    [parameters setValue:code forKey:@"validCode"];
    [parameters setValue:@"B" forKey:@"bt"];
    
    [HFHttpLJCFactory factory]
    .setDomain(DOMAIN_BANK)
    .setPath(MobileServiceVerifyValidCode)
    .setRequest(parameters)
    .setResponseBlock(^(id<HFHttpResponseModel> resp) {
        HFHttpResonseModule *module = (HFHttpResonseModule *)resp;
        if (module.state.integerValue == 0) {
            if (completionHandler) {
                completionHandler(YES, module.responseObject, nil);
            }
        } else {
            if (completionHandler) {
                completionHandler(NO, module.responseObject, nil);
            }
        }
    })
    .setErrorBock(^(id<HFHttpFailModel> error) {
        if (completionHandler) {
            completionHandler(NO, nil, error);
        }
    })
    .post();
}

+ (void)registerWithMobile:(NSString *)mobile password:(NSString *)password code:(NSString *)code completionHandler:(void(^)(BOOL success, id responseObject, NSError *error))completionHandler {
    password = [[password dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:mobile forKey:@"mobile"];
    [parameters setValue:mobile forKey:@"base_mobile"];
    [parameters setValue:password forKey:@"pass"];
    [parameters setValue:code forKey:@"validCode"];
    [parameters setValue:@"A" forKey:@"bt"];
    
    [HFHttpLJCSignFactory factory]
    .setDomain(DOMAIN_BASE)
    .setPath(MobileServiceRegistry)
    .setRequest(parameters)
    .setResponseBlock(^(id<HFHttpResponseModel> resp) {
        HFHttpResonseModule *module = (HFHttpResonseModule *)resp;
        if (module.state.integerValue == 0) {
            if (completionHandler) {
                completionHandler(YES, module.responseObject, nil);
            }
        } else {
            if (completionHandler) {
                completionHandler(NO, module.responseObject, nil);
            }
        }
    })
    .setErrorBock(^(id<HFHttpFailModel> error) {
        if (completionHandler) {
            completionHandler(NO, nil, error);
        }
    })
    .post();
}

+ (void)passwordLoginWithMobile:(NSString *)mobile password:(NSString *)password completionHandler:(void(^)(BOOL success, id responseObject, NSError *error))completionHandler {
    password = [[password dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:mobile forKey:@"mobile"];
    [parameters setValue:mobile forKey:@"base_mobile"];
    [parameters setValue:password forKey:@"pass"];
    [parameters setValue:@"pass" forKey:@"type"];
    
    [HFHttpLJCSignFactory factory]
    .setDomain(DOMAIN_USER)
    .setPath(MobileServiceLogin)
    .setRequest(parameters)
    .setResponseBlock(^(id<HFHttpResponseModel> resp) {
        HFHttpResonseModule *module = (HFHttpResonseModule *)resp;
        if (module.state.integerValue == 0) {
            if (completionHandler) {
                completionHandler(YES, module.responseObject, nil);
            }
        } else {
            if (completionHandler) {
                completionHandler(NO, module.responseObject, nil);
            }
        }
    })
    .setErrorBock(^(id<HFHttpFailModel> error) {
        if (completionHandler) {
            completionHandler(NO, nil, error);
        }
    })
    .post();
}

+ (void)resetPasswordWithMobile:(NSString *)mobile password:(NSString *)password code:(NSString *)code token:(NSString *)token completionHandler:(void(^)(BOOL success, id responseObject, NSError *error))completionHandler {
    password = [[password dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:mobile forKey:@"mobile"];
    [parameters setValue:mobile forKey:@"base_mobile"];
    [parameters setValue:password forKey:@"pass"];
    [parameters setValue:code forKey:@"validCode"];
    [parameters setValue:token forKey:@"codeTk"];
    
    [HFHttpLJCSignFactory factory]
    .setDomain(DOMAIN_BASE)
    .setPath(MobileServiceSetLoginPass)
    .setRequest(parameters)
    .setResponseBlock(^(id<HFHttpResponseModel> resp) {
        HFHttpResonseModule *module = (HFHttpResonseModule *)resp;
        if (module.state.integerValue == 0) {
            if (completionHandler) {
                completionHandler(YES, module.responseObject, nil);
            }
        } else {
            if (completionHandler) {
                completionHandler(NO, module.responseObject, nil);
            }
        }
    })
    .setErrorBock(^(id<HFHttpFailModel> error) {
        if (completionHandler) {
            completionHandler(NO, nil, error);
        }
    })
    .post();
}

+ (void)changePasswordWithOldPassword:(NSString *)oldPassword newPassword:(NSString *)newPassword completionHandler:(void(^)(BOOL success, id responseObject, NSError *error))completionHandler {
    oldPassword = [[oldPassword dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
    newPassword = [[newPassword dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:oldPassword forKey:@"oldpass"];
    [parameters setValue:newPassword forKey:@"newpass"];
    
    [HFHttpLJCSignFactory factory]
    .setDomain(DOMAIN_BASE)
    .setPath(MobileServiceResetLoginPass)
    .setRequest(parameters)
    .setResponseBlock(^(id<HFHttpResponseModel> resp) {
        HFHttpResonseModule *module = (HFHttpResonseModule *)resp;
        if (module.state.integerValue == 0) {
            if (completionHandler) {
                completionHandler(YES, module.responseObject, nil);
            }
        } else {
            if (completionHandler) {
                completionHandler(NO, module.responseObject, nil);
            }
        }
    })
    .setErrorBock(^(id<HFHttpFailModel> error) {
        if (completionHandler) {
            completionHandler(NO, nil, error);
        }
    })
    .post();
}

@end

